FundManager = new Object(); // Like class name in java

var formPanel;
var fund_manager_id = null;
var fund_manager_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'fmCode', 'fmName', 'fmBusinessRegNo', 'fmAddress', 'fmEmail', 'fmFax', 'fmPhone' ]
});

var contactPersons_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'cpName', 'cpDesignation', 'cpPhone', 'cpFax', 'cpEmail' ]
});
FundManager.getPersonalDetails = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Personal Deatails',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'Name <span style="color:red">*</span>',
			allowBlank : false,
			maxLength : 100,
			name : 'fmName',
			msgTarget : 'under',
			blankText : 'Name is required',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Code<span style="color:red">*</span>',
				flex : 1,
				allowBlank : false,
				msgTarget : 'under',
				blankText : 'Code is required',
				name : 'fmCode',
				maxLength : 30,
				padding : '0 5 0 0'
			}, {
				xtype : 'textfield',
				labelWidth : 120,
				flex : 1,
				maxLength : 30,
				name : 'fmBusinessRegNo',
				fieldLabel : 'Business Reg. No.'
			} ]
		} ]
	});
};

FundManager.getContactDetails = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Contact Deatails',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'textarea',
			fieldLabel : 'Mailing Address',
			height : 50,
			maxLength : 250,
			name : 'fmAddress'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Phone',
				name : 'fmPhone',
				maskRe : /[\d]/,
				padding : '0 5 0 0',
				flex : 1,
				minLength : 10,
				msgTarget : 'under'
			}, {
				xtype : 'textfield',
				fieldLabel : 'Fax',
				maskRe : /[\d]/,
				labelWidth : 50,
				padding : '0 5 0 0',
				flex : 1,
				minLength : 10,
				msgTarget : 'under',
				name : 'fmFax'
			}, {
				xtype : 'textfield',
				fieldLabel : 'E-mail',
				vtype : 'email',
				labelWidth : 50,
				maxLength : 150,
				flex : 1,
				msgTarget : 'under',
				name : 'fmEmail'
			} ]
		} ]
	});
};

FundManager.getContactPersons = function() {

	var fund_manager_grid_con_per = null;
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 2,
		autoCancel : false
	});

	fund_manager_grid_con_per = Ext.create('Ext.grid.Panel', {
		id : 'grid',
		store : contactPersons_store,
		layout : {
			type : 'fit'
		},
		height : 135,
		anchor : '100%',
		columns : [ {
			header : 'Name',
			dataIndex : 'cpName',
			width : 140,
			flex : 1,
			editor : {
				allowBlank : false
			}
		}, {
			header : 'Designation',
			dataIndex : 'cpDesignation',
			width : 140,
			flex : 1,
			editor : {
			// allowBlank : false
			}
		}, {
			header : 'Phone',
			dataIndex : 'cpPhone',
			width : 100,
			editor : {
				allowBlank : false
			}
		}, {
			header : 'Fax',
			dataIndex : 'cpFax',
			width : 100,
			editor : {
			// allowBlank : false
			}
		}, {
			header : 'Email',
			dataIndex : 'cpEmail',
			width : 150,
			editor : {
				allowBlank : false,
				vtype : 'email'
			}
		} ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var formContactPerson = Ext.getCmp('contactPerson').getForm();
						if (Ext.getCmp('cname').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the name.');
						} else if (Ext.getCmp('cphone').getValue() == null) {
							Ext.MessageBox.alert('Error', 'Please specify the phone.');
						} else if (Ext.getCmp('cemail').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the E-mail.');
						} else if (!formContactPerson.isValid()) {
							Ext.MessageBox.alert('Error', 'Form is Not valid');
						} else {
							contactPersons_store.add({
								cpName : Ext.getCmp('cname').getValue(),
								cpDesignation : Ext.getCmp('cdesignation').getValue(),
								cpPhone : Ext.getCmp('cphone').getValue(),
								cpFax : Ext.getCmp('cfax').getValue(),
								cpEmail : Ext.getCmp('cemail').getValue()
							});
							formContactPerson.reset();
						}
					}
				}
			}, {
				xtype : 'button',
				text : 'Remove',
				listeners : {
					click : function() {
						var selection = fund_manager_grid_con_per.getView().getSelectionModel().getSelection()[0];
						if (selection) {
							contactPersons_store.remove(selection);
						}
					}
				}
			} ]
		} ],
		plugins : [ rowEditing ]
	});
	return Ext.create('Ext.form.FieldSet', {
		title : 'Contact Persons',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'form',
			bodyBorder : false,
			frame : true,
			border : false,
			id : 'contactPerson',
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'textfield',
					id : 'cname',
					maxLength : 100,
					msgTarget : 'side',
					fieldLabel : 'Name',
					padding : '0 5 0 0',
					flex : 1
				}, {
					xtype : 'textfield',
					maxLength : 60,
					fieldLabel : 'Designation',
					id : 'cdesignation',
					flex : 1
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				fieldLabel : 'Contact Details',
				fieldDefaults : {
					labelAlign : 'top'
				},
				items : [ {
					xtype : 'textfield',
					fieldLabel : 'Phone',
					id : 'cphone',
					padding : '0 5 0 0',
					flex : 1,
					maskRe : /[\d]/,
					minLength : 10,
					msgTarget : 'under'
				}, {
					xtype : 'textfield',
					fieldLabel : 'Fax',
					id : 'cfax',
					padding : '0 5 0 0',
					flex : 1,
					maskRe : /[\d]/,
					minLength : 10,
					msgTarget : 'under'
				}, {
					xtype : 'textfield',
					fieldLabel : 'E-mail',
					id : 'cemail',
					maxLength : 150,
					vtype : 'email',
					padding : '0 5 0 0',
					flex : 1
				} ]
			} ]
		}, fund_manager_grid_con_per ]
	});
};

FundManager.getCenterPanel = function() {

	var personalDetails = FundManager.getPersonalDetails();
	var contactDetails = FundManager.getContactDetails();
	var contactPersons = FundManager.getContactPersons();

	formPanel = Ext.create('Ext.form.Panel', {
		title : 'Fund Manager',
		bodyPadding : '20',
		id : 'fund_manager_form',
		autoScroll : true,
		defaults : {
			labelWidth : 175,
			anchor : '65%'
		},
		frame : true,
		items : [ {
			xtype : 'fieldcontainer',
			items : [ {
				xtype : 'triggerfield',
				emptyText : 'Search Fund Manager',
				triggerCls : 'x-form-search-trigger',
				width : 300,
				style : 'float:right;',
				id : 'fund_manager_object',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							Ext.Msg.alert('Fund Manager', 'Invalid Code or Fund Manager Code not avilabel');
						}
					}
				},
				onTriggerClick : function() {
					var window = null;
					Ext.Ajax.request({
						url : 'getFundManagerListJson.action',
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							fund_manager_store.loadRawData(obj.fundManagerBeans);
						}
					});
					if (!window) {
						window = Ext.create('Ext.window.Window', {
							title : 'Fund Manager',
							height : 330,
							frame : true,
							width : 500,
							constrain : true,
							resizable : false,
							modal : true,
							layout : {
								type : 'fit'
							},
							items : [ {
								xtype : 'gridpanel',
								defaults : {
									anchor : '100%'
								},
								store : fund_manager_store,
								columns : [ {
									text : 'Code',
									dataIndex : 'fmCode',
									flex : 1
								}, {
									text : 'Name',
									dataIndex : 'fmName',
									flex : 1
								} ],
								listeners : {
									itemdblclick : function(dv, record, item, index, e) {
										var data = record.data;
										FundManager.setDataToForm(data, record);
										window.hide();
									}
								}
							} ]
						});
					}
					;
					if (window.isVisible()) {
						window.hide();
					} else {
						window.show();
					}

				}
			} ]
		}, personalDetails, contactDetails, contactPersons ],

		// Reset and Submit buttons
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				contactPersons_store.removeAll();
				Ext.getCmp('save').setText('Save');
				fund_manager_id = null;
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					formPanel.setLoading('Saving...');
					var grids = Ext.getCmp('grid');
					var contacts_store = [];
					grids.getStore().each(function(record) {
						var obj = {
							"cpname" : record.get("cpName"),
							"cpDesignation" : record.get("cpDesignation"),
							"cpPhone" : record.get("cpPhone"),
							"cpFax" : record.get("cpFax"),
							"cpEmail" : record.get("cpEmail")
						};
						contacts_store.push(obj);
					});
					if (fund_manager_id == null) {
						form.url = 'saveFundManager.action';
						form.submit({
							params : {
								"contactPersons" : Ext.encode(contacts_store),
							},
							success : function(form, action) {
								form.reset();
								Ext.getCmp('save').setText('Save');
								fund_manager_id = null;
								contactPersons_store.removeAll();
								Ext.Msg.alert('Success', 'Saved Successfully');
								formPanel.setLoading(false);
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', 'There is some error returned from the server.');
								formPanel.setLoading(false);
							}
						});
					} else {
						form.url = 'updateFundManager.action';
						form.submit({
							params : {
								"contactPersons" : Ext.encode(contacts_store),
								"fund_manager_id" : fund_manager_id
							},
							success : function(form, action) {
								form.reset();
								Ext.getCmp('save').setText('Save');
								fund_manager_id = null;
								contactPersons_store.removeAll();
								Ext.Msg.alert('Success', 'Updated Successfully');
								formPanel.setLoading(false);
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', 'There is some error returned from the server.');
								formPanel.setLoading(false);
							}
						});
					}
				}
			}
		} ],
		listeners : {
			render : function(p) {
				if (App.screenPermiton.getAt(0).data.create == 0)
					Ext.getCmp('save').disable();
				else
					Ext.getCmp('save').enable();
			}
		}
	});
	return formPanel;
};

FundManager.setDataToForm = function(data, record) {

	var formPanl = Ext.getCmp('fund_manager_form');
	formPanel.setLoading('Loading...');
	fund_manager_id = data.id;
	Ext.getCmp('fund_manager_object').setValue(data.fmName);

	formPanl.getForm().loadRecord(record);

	Ext.Ajax.request({
		url : 'getfmCntPersons.action',
		params : {
			"fundManagerId" : fund_manager_id
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			contactPersons_store.removeAll();
			contactPersons_store.loadRawData(obj.contactPersonBeans);
			formPanel.setLoading(false);
		}
	});
	Ext.getCmp('save').setText('Update');
};
